package com.xinqing.test.algorithm.k8s.misc;

import com.alibaba.fastjson.JSON;
import com.xinqing.test.modle.algorithm.ConvertModelDTO;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;

/**
 * @Author:CHQ
 * @Date:2020/12/24 16:25
 * @Description
 */
public class SparkConvertPmml {
    public static void main(String args[]) throws Exception {
        //创建JSON格式实体对象
        ArrayList<String> list = new ArrayList<>();
        list.add("/user/algo/guifu/niejiabin/sml_model/continue");
        ConvertModelDTO convertParam = new ConvertModelDTO
                (list, "/user/algo/guifu/niejiabin/sml_model/1.pmml");
        String data = JSON.toJSONString(convertParam);

        //请求url
        String url = "http://sml.suishoudata.com/web/ui/models/convert/spark-to-pmml";

        //模拟post请求
        HttpPost httpPost = new HttpPost(url);
        httpPost.addHeader("Content-Type", "application/json");
        httpPost.setEntity(new StringEntity(data));

        //执行post请求
        CloseableHttpClient httpClient = HttpClients.createDefault();
        CloseableHttpResponse response = httpClient.execute(httpPost);

        //输出请求结果
        HttpEntity entity = response.getEntity();
        String responseContent = EntityUtils.toString(entity, "UTF-8");
        System.out.println(responseContent);

        //资源关闭
        response.close();
        httpClient.close();
    }
}
